package com.ctgu.j08.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.j08.common.enums.ResultEnum;
import com.ctgu.j08.mapper.DeviceMapper;
import com.ctgu.j08.mapper.RepairMapper;
import com.ctgu.j08.mapper.TroubleMapper;
import com.ctgu.j08.service.RepairService;
import com.ctgu.j08.utils.ResultUtil;
import com.ctgu.j08.vo.Repair;
import com.ctgu.j08.vo.Result;
import com.ctgu.j08.vo.Trouble;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @date: 2021/6/3 10:24
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {
    @Autowired
    private RepairMapper repairMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private TroubleMapper troubleMapper;
    @Override
    public Result addRepair(Repair repair) {
        int deId = repair.getDeId();
        System.out.println("要更新状态的设备："+deId);
        int deStatus = 2;
        int trStatus = 1;
        Trouble trouble = troubleMapper.selectByDeId(deId);
        System.out.println("查到的trouble"+trouble);
        int trId = trouble.getTrId();
        //更新报修分配状态
        int isUp = troubleMapper.updateStatus(trId,trStatus);
        System.out.println("更新报修分配状态成功！"+isUp);
        //更新设备状态
        int isUpDevice = deviceMapper.updateStatus(deId,deStatus);
        System.out.println("更新设备状态成功！"+isUpDevice);
        //同时修改报修表为已经分配
        repair.setReStatus(2);
        int isAdd = repairMapper.insert(repair);
        System.out.println("插入状态"+isAdd);
        if(isAdd != 0 && isUp != 0 && isUpDevice != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
        else
        {
            System.out.println("报修失败"+isAdd);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getAllRepairInfo() {
        List<Repair> repairList = repairMapper.getAllRepairInfo();
        if(repairList == null)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }else
        {
            System.out.println("报修信息列表："+repairList);
            return ResultUtil.success(repairList);
        }
    }

    @Override
    public Result editRepair(Repair repair) {
        Integer deId = repair.getDeId();
        Integer deStatus = repair.getReStatus();
        //更新维修信息
        Integer isUp = repairMapper.updateByPrimaryKey(repair);
        //更新设备信息
        Integer isUpDevice = deviceMapper.updateStatus(deId,deStatus);

        if(isUp != 0 && isUpDevice != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }
       else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }
}
